package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：
 * @date ：Created in 2019/7/16
 * @description ：
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            System.err.println(">>>>>>>>>>>>>>"+permission.toString());
            permissionService.add(permission);
            return new Result(true,MessageConst.ADD_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_CHECKITEM_FAIL);
        }

    }

    @RequestMapping("/findPage")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
        try {
            System.err.println(">>>>>>>11111111111111111111>>>>>>>>>>>>>>>>>>>>>>"+queryPageBean.toString());
            return permissionService.pageQuery(queryPageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new PageResult(0L,new ArrayList());
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            System.err.println("<<<<<<<<<<<<<<<<<<"+id+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
            permissionService.deleteById(id);
            return  new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
       try {
           Permission byId = permissionService.findById(id);
           return new Result(true,MessageConst.ACTION_SUCCESS,byId);
       }catch (Exception e){
           e.printStackTrace();
           return  new Result(false,MessageConst.ACTION_FAIL);
       }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);

        }

    }
    @RequestMapping("/findAll")
    @ResponseBody
    public  Result findAll(){
        try {
            List<Permission> all = permissionService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,all);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }

    }




}
